package com.sunshine.user.controller;


import com.sunshine.user.entity.UserAddresses;
import com.sunshine.user.service.UserAddressesService;
import com.sunshine.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import sunshine.commcon.util.entity.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addresses")
public class UserAddressesController {

    @Resource
    private UserAddressesService userAddressesService;

    @GetMapping("/findUserAddressesByToken")
    public Result<List<UserAddresses>> findUserAddressesByToken( UserAddresses userAddresses,HttpServletRequest request) {
        Long userId = TokenUtil.getUserIdFromRequest(request);
        log.info("查询用户地址: {}", userAddresses);
        userAddresses.setUserId(userId);

        return userAddressesService.findUserAddressesByToken(userAddresses);
    }


    @PostMapping("/addUserAddress")
    public Result<Boolean> addUserAddress(@RequestBody UserAddresses userAddresses, HttpServletRequest request) {
        Long userId = TokenUtil.getUserIdFromRequest(request);
        userAddresses.setUserId(userId);
        log.info("添加用户地址: {}", userAddresses);
        return userAddressesService.addUserAddresses(userAddresses);
    }

    @DeleteMapping("/deleteAddress/{addressId}")
    public Result<Boolean> deleteAddress(@PathVariable Long addressId, HttpServletRequest request) {
        Long userId = TokenUtil.getUserIdFromRequest(request);
        log.info("删除用户地址，地址ID: {}，用户ID: {}", addressId, userId);
        return userAddressesService.deleteAddressById(addressId, userId);
    }
}